import os

import xlrd
import xlwt


def readExcel(f):
  bok = xlrd.open_workbook(f)
  return bok


def createdExcel(name, order_arr):
  wb = xlwt.Workbook()  # 创建excel文件
  sheet = wb.add_sheet(name)  # 为第一个表命名

  sheet.row(0).height_mismatch = True
  sheet.row(0).height = 30*20
  sheet.col(0).width = 256*72
  style = xlwt.XFStyle()  # 创建一个样式对象，初始化样式
  al = xlwt.Alignment()
  al.horz = 0x02      # 设置水平居中
  al.vert = 0x01      # 设置垂直居中
  style.alignment = al
  sheet.write(0, 0, '项目',style)
  sheet.write(0, 1, '项目咨询',style)
  for i in range(len(order_arr)):
    sheet.row(i+1).height_mismatch = True
    sheet.row(i+1).height = 30*20
    sheet.col(i+1).width = 256*72
    # if '项目驻场：' in order_arr[i]['phone']:
    #   newPhone = order_arr[i]['phone']
    #   newPhone = newPhone[5:len(newPhone)]
    #   sheet.write(i + 1, 0, order_arr[i]['name'])
    #   sheet.write(i + 1, 1, newPhone)
    # else:
    sheet.write(i + 1, 0, order_arr[i]['name'],style)
    sheet.write(i + 1, 1, order_arr[i]['phone'],style)
    # print('i_' + str(i), order_arr[i])


  file_path = os.path.join(os.getcwd(), 'G:\\work\\vuework\\crash-handler-management\\pythonManger\\')  # 指定要保存的目录

  if not os.path.exists(file_path):  # 如果目录不存在，生成
    os.mkdir(file_path)
  file_path2 = file_path + '楼盘全部.xls'  # 文件的绝对路径
  wb.save(file_path2)


if __name__ == '__main__':
  path = r'G:\work\vuework\crash-handler-management\pythonManger\优居总代项目明细表-fix.xlsx'
  sheets = readExcel(path)
  colx = 0
  phone_colx = 0
  all_sheets = sheets.sheet_names()
  page = 0
  order_arr = []
  for k in range(len(all_sheets)):
    page = k
    sheet = sheets.sheet_by_index(k)
    rows_data = sheet.row_values(3)
    for i in range(len(rows_data)):
      item = rows_data[i]
      if item == '项目':
        colx = i
      if '咨询' in item:
        phone_colx = i

    order_nums = sheet.col_values(colx)
    order_phone_nums = sheet.col_values(phone_colx)
    for i in range(len(order_nums)):
      if i > 3:
        item = order_nums[i]
        item_phone = order_phone_nums[i]
        if item != '':
          # print(item_phone)
          # if '驻场' in item_phone:
          #   if '经理' in item_phone:
          #     print('i_驻场_经理_'+str(i),item_phone)
          obj = {
            'name': item,
            'phone': item_phone
          }
          order_arr.append(obj)
        # print('item_'+str(i), item, item_phone)
  createdExcel('全部', order_arr)
